﻿using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace Core.Framework.CommonResults;

public class CommonResultFilter: IAsyncResultFilter
{
    public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
    {
        // 1、获取Action结果
        var result = context.Result;
        // 2、判断Action结果类型 ObjectResult
        if (result is ObjectResult objResult)
        {
            CommonResult commonResult = new CommonResult();
            if (objResult.Value is CommonResult common)
            {
                commonResult.Data = new JsonResult(common);
            }
            else
            {
                commonResult.Code = (int)HttpStatusCode.OK;
                commonResult.Msg = "";
                commonResult.Data = objResult.Value;
                context.Result = new JsonResult(commonResult);
            }
        }
        // 3、判断Action结果类型 EmptyResult
        else if (result is EmptyResult emptyResult)
        {
            CommonResult commonResult = new CommonResult();
            commonResult.Code = (int)HttpStatusCode.OK;
            commonResult.Data = null;
            commonResult.Msg = "";
            context.Result = new JsonResult(commonResult);
        }
        // 4、判断Action结果类型 JsonResult
        else if (result is JsonResult jsonResult)
        {
            CommonResult commonResult = new CommonResult();
            commonResult.Code = (int)HttpStatusCode.OK;
            commonResult.Data = jsonResult.Value;
            commonResult.Msg = "";
            context.Result = new JsonResult(commonResult);
        }
        // 5、判断Action结果类型 StatusCodeResult
        else if (result is StatusCodeResult statusCodeResult)
        {
            CommonResult commonResult = new CommonResult();
            commonResult.Code = (int)statusCodeResult.StatusCode;
            commonResult.Data = statusCodeResult.StatusCode;
            commonResult.Msg = "";
            context.Result = new JsonResult(commonResult);
        }
        else if (result is RedirectResult redirectResult)
        {
            CommonResult commonResult = new CommonResult();
            commonResult.Code = (int)HttpStatusCode.OK;
            commonResult.Data = redirectResult.Url;
            commonResult.Msg = "";
            context.Result = new JsonResult(commonResult);
        }
        else if (result is FileContentResult fileContentResult)
        {
            CommonResult commonResult = new CommonResult();
            commonResult.Code = (int)HttpStatusCode.OK;
            commonResult.Data = fileContentResult;
            commonResult.Msg = "";
            context.Result = new JsonResult(commonResult);
        }
        else if (result is UnauthorizedAccessException unauthorizedAccessException)
        {
            CommonResult commonResult = new CommonResult();
            commonResult.Code = (int)HttpStatusCode.Unauthorized;
            commonResult.Data = null;
            commonResult.Msg = "";
            context.Result = new JsonResult(commonResult);
        }

        await next();
    }
}
